<?php
/**
 * 我的评价
 *
 *
 *
 *
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class member_evaluateControl extends mobileMemberControl {

	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 订单评价（新）
	 * 商品评价
	 * 1.找到该订单购买的所有商品
	 * 2，对每一件商品分别进行评价
	 */
	public function add_evaluateOp(){
	    $order_model=Model('order');
	    //商品评价模型
	    $model_evaluate_goods = Model('evaluate_goods');
	    $order_id = intval($_POST['order_id']);
	    //评分  

	    //描述评分
	    $data['eval_des']=$_REQUEST['eval_des'];
	    if($data['eval_des'] <= 0 || $data['eval_des'] > 5) {
	        $data['eval_des'] = 5;
	    }
	    //发货速度评分,保存在订单表里
	    $data['eval_shipping']=$_REQUEST['eval_shipping'];
	    if($data['eval_shipping'] <= 0 || $data['eval_shipping'] > 5) {
	        $data['eval_shipping'] = 5;
	    }
	    //服务评分
	    $data['eval_service']=$_REQUEST['eval_service'];
	    if($data['eval_service'] <= 0 || $data['eval_service'] > 5) {
	        $data['eval_service'] = 5;
	    }
	    
	    $data['eval_order']=$_REQUEST['eval_order'];
	    if($data['eval_order'] <= 0 || $data['eval_order'] > 5) {
	        $data['eval_order'] = 5;
	    }
	    
	    $data['evaluation_state']=1;
	    $data['eval_comment']=$_REQUEST['comment'];
	    //更新订单日志
	    $state = $order_model->editOrder($data, array('order_id' => $order_id));
	    output_data(array('status'=>'success','msg'=>'操作成功！'));
	}

    /**
     * 订单添加评价
     */
    public function addOp(){
        $order_id = intval($_POST['order_id']);

        $model_order = Model('order');
        $model_store = Model('store');
        $model_evaluate_goods = Model('evaluate_goods');
        $model_evaluate_store = Model('evaluate_store');

        //获取订单信息
        $order_info = $model_order->getOrderInfo(array('order_id' => $order_id));
        //判断订单身份
        if($order_info['buyer_id'] != $this->member_info['member_id']) {
            
        }
        //订单为'已收货'状态，并且未评论
        $order_info['evaluate_able'] = $model_order->getOrderOperateState('evaluation',$order_info);
        if (empty($order_info) || !$order_info['evaluate_able']){
            
        }

        //查询店铺信息
        $store_info = $model_store->getStoreInfoByID($order_info['store_id']);
        if(empty($store_info)){
            
        }

        //获取订单商品
        $order_goods = $model_order->getOrderGoodsList(array('order_id'=>$order_id));
        if(empty($order_goods)){
            
        }

        $evaluate_goods_array = array();
            $goodsid_array = array();
            foreach ($order_goods as $value){
                //如果未评分，默认为5分
                $evaluate_score = intval($_POST['score']);
                if($evaluate_score <= 0 || $evaluate_score > 5) {
                    $evaluate_score = 5;
                }
                //默认评语
                $evaluate_comment = $_POST['comment'];
                if(empty($evaluate_comment)) {
                    $evaluate_comment = '不错哦';
                }

                $evaluate_goods_info = array();
                $evaluate_goods_info['geval_orderid'] = $order_id;
                $evaluate_goods_info['geval_orderno'] = $order_info['order_sn'];
                $evaluate_goods_info['geval_ordergoodsid'] = $value['rec_id'];
                $evaluate_goods_info['geval_goodsid'] = $value['goods_id'];
                $evaluate_goods_info['geval_goodsname'] = $value['goods_name'];
                $evaluate_goods_info['geval_goodsprice'] = $value['goods_price'];
                $evaluate_goods_info['geval_goodsimage'] = $value['goods_image'];
                $evaluate_goods_info['geval_scores'] = $evaluate_score;
                $evaluate_goods_info['geval_content'] = $evaluate_comment;
                $evaluate_goods_info['geval_isanonymous'] = $_POST['anony']?1:0;
                $evaluate_goods_info['geval_addtime'] = TIMESTAMP;
                $evaluate_goods_info['geval_storeid'] = $store_info['store_id'];
                $evaluate_goods_info['geval_storename'] = $store_info['store_name'];
                $evaluate_goods_info['geval_frommemberid'] = $this->member_info['member_id'];
                $evaluate_goods_info['geval_frommembername'] = $this->member_info['member_name'];

                $evaluate_goods_array[] = $evaluate_goods_info;
                
                $goodsid_array[] = $value['goods_id'];
            }
            //批量添加订单
            $model_evaluate_goods->addEvaluateGoodsArray($evaluate_goods_array, $goodsid_array);

            $store_desccredit = intval($_POST['store_desccredit']);
            if($store_desccredit <= 0 || $store_desccredit > 5) {
                $store_desccredit= 5;
            }
            $store_servicecredit = intval($_POST['store_servicecredit']);
            if($store_servicecredit <= 0 || $store_servicecredit > 5) {
                $store_servicecredit = 5;
            }
            $store_deliverycredit = intval($_POST['store_deliverycredit']);
            if($store_deliverycredit <= 0 || $store_deliverycredit > 5) {
                $store_deliverycredit = 5;
            }
            //添加店铺评价
            if (!$store_info['is_own_shop']) {
                $evaluate_store_info = array();
                $evaluate_store_info['seval_orderid'] = $order_id;
                $evaluate_store_info['seval_orderno'] = $order_info['order_sn'];
                $evaluate_store_info['seval_addtime'] = time();
                $evaluate_store_info['seval_storeid'] = $store_info['store_id'];
                $evaluate_store_info['seval_storename'] = $store_info['store_name'];
                $evaluate_store_info['seval_memberid'] = $this->member_info['member_id'];
                $evaluate_store_info['seval_membername'] = $this->member_info['member_name'];
                $evaluate_store_info['seval_desccredit'] = $store_desccredit;
                $evaluate_store_info['seval_servicecredit'] = $store_servicecredit;
                $evaluate_store_info['seval_deliverycredit'] = $store_deliverycredit;
            }
            $model_evaluate_store->addEvaluateStore($evaluate_store_info);

            //更新订单信息并记录订单日志
            $state = $model_order->editOrder(array('evaluation_state'=>1), array('order_id' => $order_id));
            $model_order->editOrderCommon(array('evaluation_time'=>TIMESTAMP), array('order_id' => $order_id));
            if ($state){
                $data = array();
                $data['order_id'] = $order_id;
                $data['log_role'] = 'buyer';
                $data['log_msg'] = L('order_log_eval');
                $model_order->addOrderLog($data);
            }

            //添加会员积分
            if (C('points_isuse') == 1){
                $points_model = Model('points');
                $points_model->savePointsLog('comments',array('pl_memberid'=>$this->member_info['member_id'],'pl_membername'=>$this->member_info['member_id']));
            }
            //添加会员经验值
            Model('exppoints')->saveExppointsLog('comments',array('exp_memberid'=>$this->member_info['member_id'],'exp_membername'=>$this->member_info['member_name']));
            output_data(array('msg'=>'操作成功！'));
    }

    /**
     * 虚拟商品评价
     */
    public function add_vrOp(){
        $order_id = intval($_POST['order_id']);
        if (!$order_id){
            output_error('参数错误!');
        }

        $model_order = Model('vr_order');
        $model_store = Model('store');
        $model_evaluate_goods = Model('evaluate_goods');
        $model_evaluate_store = Model('evaluate_store');
    
        //获取订单信息
        $order_info = $model_order->getOrderInfo(array('order_id' => $order_id));
        //判断订单身份
        if($order_info['buyer_id'] != $this->member_info['member_id']) {
            
        }
        //订单为'已收货'状态，并且未评论
        $order_info['evaluate_able'] = $model_order->getOrderOperateState('evaluation',$order_info);
        if (!$order_info['evaluate_able']){
            
        }

        //查询店铺信息
        $store_info = $model_store->getStoreInfoByID($order_info['store_id']);
        if(empty($store_info)){
            
        }
        $order_goods = array($order_info);
    
        $evaluate_goods_array = array();
            $goodsid_array = array();
            foreach ($order_goods as $value){
                //如果未评分，默认为5分
                $evaluate_score = intval($_POST['score']);
                if($evaluate_score <= 0 || $evaluate_score > 5) {
                    $evaluate_score = 5;
                }
                //默认评语
                $evaluate_comment = $_POST['comment'];
                if(empty($evaluate_comment)) {
                    $evaluate_comment = '不错哦';
                }
                $evaluate_goods_info = array();
                $evaluate_goods_info['geval_orderid'] = $order_id;
                $evaluate_goods_info['geval_orderno'] = $order_info['order_sn'];
                $evaluate_goods_info['geval_ordergoodsid'] = $order_id;
                $evaluate_goods_info['geval_goodsid'] = $value['goods_id'];
                $evaluate_goods_info['geval_goodsname'] = $value['goods_name'];
                $evaluate_goods_info['geval_goodsprice'] = $value['goods_price'];
                $evaluate_goods_info['geval_goodsimage'] = $value['goods_image'];
                $evaluate_goods_info['geval_scores'] = $evaluate_score;
                $evaluate_goods_info['geval_content'] = $evaluate_comment;
                $evaluate_goods_info['geval_isanonymous'] = $_POST['anony']?1:0;
                $evaluate_goods_info['geval_addtime'] = TIMESTAMP;
                $evaluate_goods_info['geval_storeid'] = $store_info['store_id'];
                $evaluate_goods_info['geval_storename'] = $store_info['store_name'];
                $evaluate_goods_info['geval_frommemberid'] = $this->member_info['member_id'];
                $evaluate_goods_info['geval_frommembername'] = $this->member_info['member_name'];
                $evaluate_goods_array[] = $evaluate_goods_info;
                $goodsid_array[] = $value['goods_id'];
            }
            $model_evaluate_goods->addEvaluateGoodsArray($evaluate_goods_array, $goodsid_array);
    
            //更新订单信息并记录订单日志
            $state = $model_order->editOrder(array('evaluation_state'=>1,'evaluation_time'=>TIMESTAMP), array('order_id' => $order_id));
    
            //添加会员积分
            if (C('points_isuse') == 1){
                $points_model = Model('points');
                $points_model->savePointsLog('comments',array('pl_memberid'=>$this->member_info['member_id'],'pl_membername'=>$this->member_info['member_name']));
            }
            //添加会员经验值
            Model('exppoints')->saveExppointsLog('comments',array('exp_memberid'=>$this->member_info['member_id'],'exp_membername'=>$this->member_info['member_name']));
            output_data(array('msg'=>'操作成功！'));
    }

}